home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / PRGMANIA / BFED.10 / FILES.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-27  |  6.5 KB  |  351 lines

  1. /*
  2.     file: files.c
  3.     utility:
  4.     date: 1989
  5.     author: Jim Charlton
  6.     modifications:
  7.         1995: C. Moreau: 
  8.     comments: 
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #ifdef __PUREC__ 
  15. #include <aes.h>
  16. #include <tos.h>
  17. #include <vdi.h>
  18. #include <compend.h>
  19. #include <toserr.h>
  20. #else
  21. #include <aesbind.h>
  22. #include <osbind.h>
  23. #include <vdibind.h>
  24. #endif
  25.  
  26. #include "e:\proging\c\libs\malib\alert.h"
  27. #include "e:\proging\c\libs\malib\fselect.h"
  28.  
  29. #include "edit.h"
  30. #include "bufman.h"
  31. #include "files.h"
  32. #include "wind.h"
  33.  
  34. #include "bfed_rsc.h"
  35.  
  36. /*
  37.     globals vars
  38. */
  39. char    app_path[PATH_MAX];            /* current application path */
  40.  
  41. /*
  42.     locals vars
  43. */
  44. static char    lastpath[PATH_MAX];    /* Last path used */
  45. static char    fs_insel[PATH_MAX];    
  46. static char    fs_inpath[PATH_MAX];
  47.  
  48. /*
  49.     name: read_file
  50.     utility: 
  51.     comment: 
  52.     parameters:
  53.     return:
  54.     date: 1989
  55.     author: Jim Charlton
  56.     modifications:
  57.         1995: C. Moreau: 
  58. */
  59. int read_file(windowptr    thewin)
  60. {
  61.     int fildes, ret = TRUE;
  62.     linkbufptr    bufptr;
  63.  
  64.     graf_mouse(BUSY_BEE,0L);
  65.  
  66.     if ((fildes = open(thewin->title, O_RDWR)) != -1 )
  67.     {
  68.         const long flenr = Fseek(0L, fildes, SEEK_END);        /* get file length */
  69.  
  70.         Fseek(0L, fildes, SEEK_SET);    /* rewind */
  71.  
  72.         if (flenr >= 1)
  73.         {
  74.             long red, toread;
  75.         
  76.             for (toread=flenr; toread>0; toread-=BLOCKSIZE)
  77.             {
  78.                 long readnow = (toread < BLOCKSIZE) ? toread : BLOCKSIZE;
  79.  
  80.                 bufptr = addmember(thewin);
  81.                 if (bufptr)  /* memory available  */
  82.                 {        /* read file to ram */
  83.                     if( (red = Fread(fildes,readnow,bufptr->block)) > 0 )
  84.                     {
  85.                         bufptr->inuse = red;
  86.                         thewin->flen += red;
  87.                     }      
  88.                     else
  89.                     {
  90.                         graf_mouse(ARROW,0L);
  91.                         rsc_alert(READ_ERR);
  92.                         ret = FALSE;
  93.                         break;
  94.                     }
  95.                 }
  96.                 else    
  97.                 {
  98.                     graf_mouse(ARROW,0L);
  99.                     rsc_alert(NOMEM_2);
  100.                     ret = FALSE;
  101.                     break;
  102.                 }
  103.             }
  104.             close(fildes);
  105.  
  106.                 /* put extra char 00 at end of file  */
  107.             if(thewin->flen > 0)
  108.             {
  109.                 if(bufptr->inuse < BLOCKSIZE)
  110.                 {
  111.                     bufptr->inuse++;
  112.                     thewin->flen++;
  113.                 }
  114.                 else
  115.                 {
  116.                     bufptr = addmember(thewin); 
  117.                     if (!bufptr)  /* if no memory available  */
  118.                     {
  119.                         graf_mouse(ARROW,0L);
  120.                          rsc_alert(NOMEM_3);
  121.                          return FALSE;
  122.                     }
  123.                     else                
  124.                     {
  125.                         bufptr->inuse++;
  126.                         thewin->flen++;
  127.                     }
  128.                 }    
  129.              }
  130.           }
  131.         else
  132.         {
  133.             graf_mouse(ARROW,0L);
  134.             rsc_alert(READ_ERR2);
  135.             return FALSE;
  136.         }
  137.     }
  138.     else
  139.     {
  140.         graf_mouse(ARROW,0L);
  141.         rsc_alert(READ_ERR2);
  142.         return FALSE;
  143.     }
  144.     
  145.     graf_mouse(ARROW,0L);
  146.     return(ret);
  147. }
  148.  
  149. /*
  150.     name: write_file
  151.     utility: 
  152.     comment: 
  153.     parameters:
  154.     return:
  155.     date: 1989
  156.     author: Jim Charlton
  157.     modifications:
  158.         1995: C. Moreau: 
  159. */
  160. void write_file(windowptr thewin)
  161. {
  162.     char *string;
  163.     
  164.     rsrc_gaddr(R_STRING, S_SAVEFILE, &string);
  165.  
  166.     if (getfile(thewin, string))
  167.          save_file(thewin);
  168.  
  169. /*
  170.     name: save_file
  171.     utility: 
  172.     comment: 
  173.     parameters:
  174.     return:
  175.     date: 1989
  176.     author: Jim Charlton
  177.     modifications:
  178.         1995: C. Moreau: 
  179. */
  180. void save_file(windowptr    thewin)
  181. {
  182.     if (thewin->flen >= 2) 
  183.     {             
  184.         int fildes;    
  185.  
  186.         graf_mouse(BUSYBEE, 0L);
  187.             /* delete the dummy byte at end of file    */    
  188.             /*    the 0 flag in the delete_one call allows for delete last byte    */
  189.         delete_one(thewin,thewin->flen-1,0);
  190.     
  191.         fildes = open(thewin->title, O_RDONLY);        /* If a same file already exist */
  192.         if (fildes > 0)
  193.         {
  194.             close(fildes);
  195.  
  196.             graf_mouse(ARROW, 0L);
  197.             if ( rsc_alert(FILE_EXIST) == 2)
  198.                 return;
  199.             graf_mouse(BUSYBEE, 0L);
  200.  
  201.             Fdelete(thewin->title);
  202.         }
  203.         
  204.         fildes = creat(thewin->title, O_RDWR);
  205.         if(fildes != -1)
  206.         {
  207.             linkbufptr amem = thewin->headptr;
  208.             int error=E_OK;
  209.  
  210.             while (amem && error >= E_OK)
  211.             {
  212.                 error = (int)Fwrite(fildes, amem->inuse, amem->block);
  213.                  if (error < E_OK)
  214.                 {
  215.                     graf_mouse(ARROW,0L);
  216.                     form_error(-31-error);
  217.                     graf_mouse(BUSYBEE,0L);
  218.                 }
  219.                 amem=amem->next;
  220.             }
  221.  
  222.             close(fildes);
  223.             thewin->changed = FALSE;
  224.     
  225.             /* reinsert the Null byte at end of buffer copy of file     */
  226.                 /* step to the last buffer block in list    */
  227.             for(amem=thewin->headptr;amem->next;amem=amem->next);
  228.             
  229.             if(amem->inuse < BLOCKSIZE)    /* can put it in an existing block */
  230.             {
  231.                 amem->inuse ++;
  232.                 *(amem->block + amem->inuse) = (char)0;
  233.                  thewin->flen ++;
  234.             }
  235.             else
  236.             {
  237.                 amem = addmember(thewin); 
  238.                 if (!amem)  /* no more memory available  */
  239.                 {
  240.                     graf_mouse(ARROW, 0L);
  241.                     rsc_alert(NO_LASTBYTE);
  242.                 }
  243.                 else                
  244.                 {
  245.                     amem->inuse ++;
  246.                     *(amem->block + amem->inuse) = (char)0;
  247.                     thewin->flen ++;
  248.                 }
  249.             }    
  250.         }
  251.         else
  252.         {
  253.             graf_mouse(ARROW,0L);
  254.             rsc_alert(SAVE_ERR);
  255.             return;
  256.         }
  257.         graf_mouse(ARROW, 0L);
  258.     }
  259.     else
  260.     {
  261.         rsc_alert(SAVE_NULL);
  262.     }
  263. }
  264.  
  265. /*
  266.     name: getfile
  267.     utility: let the user to choose a file name
  268.     comment: 
  269.     parameters: thewin
  270.                 string: Name of the fileselector.
  271.     return: 1 with name in thewin->title, 0 with cancel
  272.     date: 1989
  273.     author: Jim Charlton
  274.     modifications:
  275.         jun 1996: C. Moreau: fsel_input become fileselect to use new
  276.          fsel_exinput(TOS>1.04). Add name.
  277. */
  278. int getfile(windowptr thewin, char *string)
  279. {
  280.     int fs_button = fsel_autoinput("*.*", fs_insel, fs_inpath, string);
  281.         
  282.     if (fs_button==FSEL_OK)
  283.     {
  284.         strcpy(thewin->title, fs_inpath); /* copy pathname to title */
  285.         wind_set(thewin->handle, WF_NAME, thewin->title, 0, 0);
  286.         return(1);
  287.     }
  288.     else
  289.           return(0);
  290. }
  291.  
  292. /*
  293.     name: delfile
  294.     utility: let the user to choose a file to be deleted
  295.     comment: 
  296.     parameters:
  297.     return: 1 with del, 0 with cancel 
  298.     date: 1989
  299.     author: Jim Charlton
  300.     modifications:
  301.         Jun 1996: C. Moreau: fsel_input become fileselect to use new
  302.          fsel_exinput(TOS>1.04).
  303. */
  304. int delfile(void)  
  305. {
  306.     int error;
  307.     int fs_button;
  308.     char *string;
  309.     
  310.     rsrc_gaddr(R_STRING, S_DELFILE, &string);
  311.     fs_button = fsel_autoinput("*.*", fs_insel, fs_inpath, string);
  312.  
  313.     if (fs_button==FSEL_OK)
  314.     {
  315.         graf_mouse(BUSY_BEE, 0L);
  316.         error = Fdelete(fs_inpath);
  317.         graf_mouse(ARROW, 0L);
  318.  
  319.         if (error)
  320.             form_error(-31-error);
  321.         else
  322.             return 1;
  323.     }
  324.  
  325.     return(0);
  326. }
  327.  
  328. /*
  329.     name: init_files
  330.     utility: initialize vars of files.c module
  331.     comment: 
  332.     parameters: none
  333.     return: none
  334.     date: 10 may 96
  335.     author: C.Moreau
  336.     modifications:
  337. */
  338. void init_files(void)
  339. {
  340.     char *string;
  341.     
  342.         /* Get Default path for files */
  343.     Dgetpath(lastpath, DEFAULT_DRIVE);
  344.     sprintf(fs_inpath,"%c:%s\\*.*", 'A' + Dgetdrv(), lastpath);
  345.  
  346.     rsrc_gaddr(R_STRING, S_UNTITLED, &string);
  347.     
  348.     strcpy(fs_insel, string);
  349. }
  350.